package org.daochong.fastdev.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.daochong.fastdev.Context;
import org.daochong.fastdev.OperationId;
import org.daochong.lang.BeanUtil;
import org.daochong.lang.StringUtils;

import com.alibaba.fastjson.JSON;

public class FastDevUtils {

	public static boolean operEntity(String oper, String prop) {
		if (StringUtils.isEmpty(oper) || StringUtils.isEmpty(prop))
			return false;
		if (oper.equals(prop)) {
			return true;
		} else if (prop.startsWith(oper + ".") && !prop.equals(oper + ".") && prop.split("\\.").length == 2) {
			return true;
		} else {
			return Pattern.matches("(" + oper + ")(\\d)\\..*", prop);
		}
	}

	public static boolean operNumber(String oper, String prop) {
		if (StringUtils.isEmpty(oper) || StringUtils.isEmpty(prop))
			return false;
		return oper.equals(prop) || prop.replaceAll("\\d", "").equals(oper);
	}

	public static List<OperationId> operationIds(String prop) {
		List<OperationId> list = new ArrayList<OperationId>();
		if (!StringUtils.isEmpty(prop)) {
			for (String str : prop.split(",")) {
				list.add(operationId(str.trim()));
			}
		}
		return list;
	}

	public static OperationId operationId(String prop) {
		OperationId id = new OperationId();
		id.setId(prop);
		if (prop.indexOf(":") > 0) {
			String[] ar = prop.split(":");
			id.setId(ar[0]);
			List<String> flags = new ArrayList<String>(ar.length - 1);
			for (int i = 1; i < ar.length; i++) {
				System.out.println(ar[i].trim());
				flags.add(ar[i].trim());
			}
			id.setFlags(flags);
		}
		return id;
	}


	public static String objectToString(Object o) {
		if (o == null)
			return null;
		try {
			if (o instanceof String)
				return (String) o;
			return "base64:" + StringUtils.base64Encode(BeanUtil.serializable(o));
		} catch (Throwable e) {
			return "json:" + o.getClass().getName() + ":" + JSON.toJSONString(o);
		}
	}

	public static Object stringToObject(String str) {
		if (str == null)
			return null;
		if (str.startsWith("base64:")) {
			return BeanUtil.unSerializable(StringUtils.base64Decode(str.substring(7)));
		} else if (str.startsWith("json:")) {
			str = str.substring(5);
			int pos = str.indexOf(":");
			if (pos == -1)
				return null;
			String cls = str.substring(0, pos);
			Class<?> clazz = null;
			try {
				clazz = Class.forName(cls);
				String txt = str.substring(pos + 1).trim();
				if (txt.startsWith("[")) {
					return JSON.parseArray(txt, clazz);
				} else {
					return JSON.parseObject(txt, clazz);
				}
			} catch (Throwable e) {
				throw new RuntimeException("unserializable fail", e);
			}
		} else {
			return str;
		}
	}

	
	public static void main(String[] args) throws Exception {
		Context context = new Context();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", "1");
		params.put("name", "刘曦姐");
		context.addParameters(params);
		Map<String, Object> c = new HashMap<String, Object>();
		c.put("b", "123");
		OperationId oid = new OperationId();
		oid.setId("AAA");
		c.put("oid", oid);
		context.setAttribute("a", c);

		String str = "select * from pub_users where id=#id# and name like concat('%',#name#,'%') and c =${a.oid.id}";
		System.out.println(BeanUtil.formatString(str, context));
	}
}
